package fr.charlott.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.charlott.business.dto.HotesseDTO;
import fr.charlott.business.dto.ReunionDTO;
import fr.charlott.business.service.ReunionService;
import fr.charlott.core.repository.model.Hotesse;
import fr.charlott.core.repository.model.Reunion;

@Service("reunionService")
public class ReunionServiceImpl implements ReunionService {

    private fr.charlott.core.service.ReunionService reunionService;

    private DozerBeanMapper dozerMapper;

    public ReunionServiceImpl() {
        super();
    }

    @Autowired
    public ReunionServiceImpl(fr.charlott.core.service.ReunionService reunionService, DozerBeanMapper dozerMapper) {
        this();
        this.reunionService = reunionService;
        this.dozerMapper = dozerMapper;
    }

    @Override
    @Transactional
    public void ajouterReunion(ReunionDTO reunionDTO) {
        reunionService.ajouterReunion(dozerMapper.map(reunionDTO, Reunion.class));
    }

    @Override
    @Transactional(readOnly = true)
    public List<ReunionDTO> chargerToutesLesReunions() {
        List<ReunionDTO> listeReunions = new ArrayList<ReunionDTO>();
        for (Reunion reunion : reunionService.chargerToutesLesReunions()) {
            listeReunions.add(dozerMapper.map(reunion, ReunionDTO.class));
        }
        return listeReunions;
    }

    @Override
    @Transactional
    public void importerReunion(ReunionDTO reunionDTO) {
        reunionService.ajouterReunion(dozerMapper.map(reunionDTO, Reunion.class));
    }

    @Override
    @Transactional
    public void supprimerReunion(ReunionDTO reunionDTO) {
        if (reunionService.chercherParId(reunionDTO.getId()) != null) {
            reunionService.supprimerReunion(dozerMapper.map(reunionDTO, Reunion.class));
        }
    }

    @Override
    @Transactional
    public void supprimerToutesLesReunions() {
        reunionService.supprimerToutesLesReunions();
    }

    @Override
    @Transactional(readOnly = true)
    public ReunionDTO chercherParId(Long id) {
        return dozerMapper.map(reunionService.chercherParId(id), ReunionDTO.class);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ReunionDTO> chercherParHotesse(HotesseDTO hotesseDTO) {
        List<ReunionDTO> listeReunions = new ArrayList<ReunionDTO>();
        for (Reunion reunion : reunionService.chercherParHotesse(dozerMapper.map(hotesseDTO, Hotesse.class))) {
            listeReunions.add(dozerMapper.map(reunion, ReunionDTO.class));
        }
        return listeReunions;
    }

    @Override
    @Transactional(readOnly = true)
    public double chargerCAParMois(int mois) {
        // return reunionService.loadCAByMonth(mois);
        return 0;
    }

    // getters - setters
    /**
     * @param dozerMapper
     *            the dozerMapper to set
     */
    public void setDozerMapper(DozerBeanMapper dozerMapper) {
        this.dozerMapper = dozerMapper;
    }

    /**
     * @param reunionService
     *            the reunionService to set
     */
    public void setReunionService(fr.charlott.core.service.ReunionService reunionService) {
        this.reunionService = reunionService;
    }

}
